package com.sc.nft.dao;


import com.meta.component.base.api.CommonResult;
import com.meta.user.client.api.UserBindThirdClient;
import com.meta.user.client.enums.ThirdTypeEnum;
import com.meta.user.client.req.UserBindThirdReq;
import com.meta.user.client.res.UserBindThirdRes;
import com.sc.nft.entity.WxMiniUserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;

/**
 * @author JYT
 * @create 2024/5/6 10:37
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class WxMiniUserInfoDao {

    private final UserBindThirdClient userBindThirdClient;

    public WxMiniUserInfo findByParam(@NonNull UserBindThirdReq param) {
        // 使用远程接口兼容旧逻辑
        CommonResult<UserBindThirdRes> result = userBindThirdClient.findByParam(param);
        WxMiniUserInfo info = new WxMiniUserInfo();
        BeanUtils.copyProperties(result.getData(), info);
        return info;
    }

    public WxMiniUserInfo getById(long id) {
        return findByParam(defaultUserBindThirdReq().setId(id));
    }

    public WxMiniUserInfo getByOpenId(String openId) {
        return findByParam(defaultUserBindThirdReq().setOpenId(openId));
    }

    public WxMiniUserInfo getByUserId(long userId) {
        return findByParam(defaultUserBindThirdReq().setUserId(userId));
    }

    /**
     * 兼容逻辑，固定指定 channelId = 1 （玛特宇宙）
     */
    private UserBindThirdReq defaultUserBindThirdReq() {
        return new UserBindThirdReq().setType(ThirdTypeEnum.WEIXIN);
    }
}
